#include "Log.h"
#include "LogManager.h"

namespace egg {

    Log::Log(const string& name, bool debugOutput, bool fileOutput) :
    mDebugOutput(debugOutput),
    mFileOutput(fileOutput),
    mToggle(true),
    mTimeStamp(true){
		if(name=="")
			mName=StringUtil::ptr2string(this);
		else
			mName=name;
        if (mFileOutput)
            mfpLog.open(name.c_str());
    }

    Log::~Log() {
        if (!mFileOutput) {
            mfpLog.close();
        }
    }
    
    void Log::toggle(bool t){
        mToggle=t;
    }

    void Log::logMessage(const string& message) {
        if (mToggle) {
            if (mDebugOutput)
                std::cerr << message << std::endl;
            if (mFileOutput) {
                if (mTimeStamp) {
                    struct tm *pTime;
                    time_t ctTime;
                    time(&ctTime);
                    pTime = localtime(&ctTime);
                    mfpLog << std::setw(2) << std::setfill('0') << pTime->tm_hour
                            << ":" << std::setw(2) << std::setfill('0') << pTime->tm_min
                            << ":" << std::setw(2) << std::setfill('0') << pTime->tm_sec
                            << ": ";
                }
                mfpLog << message << std::endl;
                mfpLog.flush();
            }
        }
    }

    void Log::setTimeStamp(bool timeStamp) {
        mTimeStamp = timeStamp;
    }

    void Log::setFileOutput(bool fileOutput) {
        mFileOutput = fileOutput;
    }

    void Log::setDebugOutput(bool debugOutput) {
        mDebugOutput = debugOutput;
    }

    const string& Log::getName() const {
        return mName;
    }

}
